package org.csu.jpetstore_front.service.Impl;

import org.csu.jpetstore_front.common.CommonResponse;
import org.csu.jpetstore_front.common.ResponseCode;
import org.csu.jpetstore_front.entity.Account;
import org.csu.jpetstore_front.entity.Profile;
import org.csu.jpetstore_front.entity.SignOn;
import org.csu.jpetstore_front.persistence.AccountMapper;
import org.csu.jpetstore_front.persistence.ProfileMapper;
import org.csu.jpetstore_front.persistence.SignOnMapper;
import org.csu.jpetstore_front.service.AccountService;
import org.csu.jpetstore_front.util.JWTUtils;
import org.csu.jpetstore_front.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;




@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private SignOnMapper signOnMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ProfileMapper profileMapper;

    @Override
    public CommonResponse<AccountVO> login(AccountVO accountVO){

        SignOn signOn = signOnMapper.selectById(accountVO.getUsername());
        Account account = accountMapper.selectById(accountVO.getUsername());
        Profile profile = profileMapper.selectById(accountVO.getUsername());

        if (signOn==null)
        {
            return CommonResponse.createForSuccessMessage("用户名不存在");
        }

        if (accountVO.getPassword().equals(signOn.getPassword()))
        {
            accountVO.setToken(JWTUtils.createToken(signOn.getUsername()));

            accountVO.setCountry(account.getCountry());
            accountVO.setCity(account.getCity());
            accountVO.setAddr1(account.getAddress1());
            accountVO.setAddr2(account.getAddress2());
            accountVO.setEmail(account.getEmail());
            accountVO.setPhone(account.getTelephone());
            accountVO.setState(account.getState());
            accountVO.setStatus(account.getStatus());
            accountVO.setZip(account.getZip());
            accountVO.setFirstName(account.getFirstName());
            accountVO.setLastName(account.getLastName());

            accountVO.setFavouriteCategoryId(profile.getFavCategory());
            accountVO.setLanguagePreference(profile.getLangpref());
        }



        return CommonResponse.createForSuccess(accountVO);
    }

    @Override
    public CommonResponse<AccountVO> register(AccountVO accountVO) {

        if (accountVO.getUsername()!=null&&accountVO.getPassword()!=null)
        {
            SignOn signOn = signOnMapper.selectById(accountVO.getUsername());
            if (signOn==null)
            {
                signOn = new SignOn();
                signOn.setUsername(accountVO.getUsername());
                signOn.setPassword(accountVO.getPassword());
                signOnMapper.insert(signOn);

                Account account = new Account();
                account.setUsername(accountVO.getUsername());
                account.setEmail(account.getEmail());
                account.setFirstName(accountVO.getFirstName());
                account.setLastName(account.getLastName());
                account.setStatus("0");
                account.setAddress1(accountVO.getAddr1());
                account.setAddress2(accountVO.getAddr2());
                account.setCity(accountVO.getCity());
                account.setState(accountVO.getState());
                account.setZip(accountVO.getZip());
                account.setCountry(accountVO.getCountry());
                account.setTelephone(accountVO.getPhone());
                accountMapper.insert(account);

                Profile profile = new Profile();
                profile.setUserName(accountVO.getUsername());
                profile.setFavCategory(accountVO.getFavouriteCategoryId());
                profile.setLangpref(accountVO.getLanguagePreference());
                profileMapper.insert(profile);

                return CommonResponse.createForSuccessMessage("注册成功");
            }
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(),"用户名重复");

        }
        return CommonResponse.createForErrorMessage("注册失败");
    }

    @Override
    public CommonResponse<String> checkUsernameIsExists(String username) {
        SignOn signOn = signOnMapper.selectById(username);
        if (signOn==null)
        {
            return CommonResponse.createForSuccessMessage("用户名未重复");
        }
        else
        {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名已存在");
        }

    }

    @Override
    public CommonResponse<AccountVO> changeAccountInformation(AccountVO accountVO) {
        SignOn signOn = signOnMapper.selectById(accountVO.getUsername());
        //若用户名未重复
        if (signOn==null)
        {
            signOn = new SignOn();
            signOn.setUsername(accountVO.getUsername());
            signOn.setPassword(accountVO.getPassword());
            signOnMapper.updateById(signOn);

            Account account = new Account();
            account.setUsername(accountVO.getUsername());
            account.setEmail(account.getEmail());
            account.setFirstName(accountVO.getFirstName());
            account.setLastName(account.getLastName());
            //表示账户未封禁
            account.setStatus(accountVO.getStatus());
            account.setAddress1(accountVO.getAddr1());
            account.setAddress2(accountVO.getAddr2());
            account.setCity(accountVO.getCity());
            account.setState(accountVO.getState());
            account.setZip(accountVO.getZip());
            account.setCountry(accountVO.getCountry());
            account.setTelephone(accountVO.getPhone());
            accountMapper.updateById(account);

            Profile profile = new Profile();
            profile.setUserName(accountVO.getUsername());
            profile.setFavCategory(accountVO.getFavouriteCategoryId());
            profile.setLangpref(accountVO.getLanguagePreference());
            profileMapper.updateById(profile);

            return CommonResponse.createForSuccessMessage("用户信息更新成功");
        }
        return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户名重复");
    }

    @Override
    public CommonResponse<AccountVO> findAccount(String username) {
        SignOn signOn = signOnMapper.selectById(username);

        if (signOn==null)
        {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "用户不存在");
        }

        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(signOn.getUsername());
        accountVO.setPassword(signOn.getPassword());

        Account account = accountMapper.selectById(username);
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddr1(account.getAddress1());
        accountVO.setAddr2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setZip(account.getZip());
        accountVO.setCountry(account.getCountry());
        accountVO.setPhone(account.getTelephone());

        Profile profile = profileMapper.selectById(username);
        accountVO.setLanguagePreference(profile.getLangpref());
        accountVO.setFavouriteCategoryId(profile.getFavCategory());
        accountVO.setListOption(profile.getMylistopt());
        accountVO.setBannerName(profile.getBanneropt());

        return CommonResponse.createForSuccess(accountVO);
    }
}